<?php
/**
 * 供货商管理模型
 * @author cjli
 *updateSuppliersInfo
 */
class SuppliersModel extends Model
{
	/**
	 * 更新供货商
	 * @param array $post post数据
	 * @param int $suppliers_id 供货商ID
	 */
	public static function updateSuppliersInfo($post, $suppliers_id=0)
	{
		$supplier_db = D('suppliers');
		$data = array(
			'suppliers_name' => $post['suppliers_name'],
			'breadth' => $post['breadth'],
			'material' => $post['material'],
			'lining_name' => $post['lining_name'],	
			'suppliers_desc' => $post['suppliers_desc'],
			'is_check' => intval($post['is_check']),
            'is_lined' => intval($post['is_lined']),
			'isspec' => intval($post['isspec']),
			'shirt_line' =>trim($post['shirt_line']),
			'figure'     =>intval($post['figure']),
			'brand' => $post['brand'],
			'suppliers_type' => intval($post['suppliers_type']),
			'grooves' => $post['grooves'],
			'admin' => $post['admin']
		);
		if($suppliers_id){
			$supplier_db->where(array('suppliers_id' => $suppliers_id))->save($data);
		}else{
			$suppliers_id = $supplier_db->add($data);
		}
		S('suppliers-list', NULL);
		return $suppliers_id;
	}
	
	/**
	 * 更新供货商与管理员的关联
	 * @param array $admins
	 * @param int $suppliers_id 供货商ID
	 */
	public static function updateSuppliersAdmin($adminIds, $suppliers_id)
	{
		$admin_db = D('admin');
		$where['id'] = array('in', join(',', $adminIds));
		$admin_db->where(array('suppliers_id' => $suppliers_id))->save(array('suppliers_id' => 0));
		$admin_db->where($where)->save(array('suppliers_id' => $suppliers_id));
	}
	
	/**
	 * 更新供货商跟产品等关联
	 * @param int $suppliers_id 供货商ID
	 */
	public static function updateSuppliersRelation($suppliers_id)
	{
		$product_db = D('product');
	}
	
	/**
	 * 获取供货商列表--用于下拉
	 * Enter description here ...
	 */
	public static function getSuppliersList()
	{
		if(S('suppliers-list')){
			$suppliersList = S('suppliers-list');
		}else{
			$suppliersList = array();
			$supplier_db = D('suppliers');
			$suppliersArray = $supplier_db->field('suppliers_id, suppliers_name')->where('is_check=1')->select();
			if($suppliersArray){
				foreach ($suppliersArray as $supp){
					$suppliersList[$supp['suppliers_id']] = $supp['suppliers_name'];
				}
			}
			//S('suppliers-list', $suppliersList);
		}
		return $suppliersList;
	}
	
	/**
	 * 获取供货商详细信息
	 * @param int $suppliers_id 供货商ID
	 */
	public static function getSuppliersInfo($suppliers_id)
	{
		$supplier_db = D('suppliers');
		$suppliersInfo = $supplier_db->find($suppliers_id);
		return $suppliersInfo;
	}
	
	/**
	 * 删除供货商信息
	 * @param int $suppliers_id 供货商ID
	 */
	public static function deleteSuppliersInfo($suppliers_id)
	{
		$supplier_db = D('suppliers');
		$supplier_db->delete($suppliers_id);
		
		$admin_db = D('admin');
		$admin_db->where(array('suppliers_id' => $suppliers_id))->save(array('suppliers_id' => 0));
		$product_db = D('product');
		$product_db->where(array('suppliers_id' => $suppliers_id))->save(array('suppliers_id' => 0));
	}

	/**
	 * 用料记录增加
	 * @param array $post 
	 */
	public static function inventoryAdd($post)
	{
		$type = $post['type'];
		$tid = $post['tid'];

        $amount = floatval($post['amount']);
        $change  = $amount >= 0 ? 1 : 0;
        $amount = abs($amount);
        
        $inventory_db = D('inventory');
    	$increaseAmount = $inventory_db->where(array('type'=>$type,'tid'=>$tid,'change'=>1))
    	             ->getField('sum(amount)');           
    	$decreaseAmount = self::inventoryDecrease($type,$tid);
        $before = floatval($increaseAmount) - floatval($decreaseAmount);  

    	$filter = array(
	        'type'     => $post['type'],
	        'tid'      => $post['tid'],
	        'order_sn' => isset($post['order_sn']) ? $post['order_sn'] : '',
	        'order_id' => isset($post['order_id']) ? $post['order_id'] : 0,
	        'order_item_id' => isset($post['order_item_id']) ? $post['order_item_id'] : 0,
	        'pid'    => isset($post['pid']) ? $post['pid'] : 0,
	        'before' => $before,
	        'change' => $change,
	        'amount' => $amount,
	        'remark' => isset($post['remark']) ? $post['remark'] : '',
	        'recordTime' => time(),
		);

	    return D('inventory')->add($filter);
    } 

	/**
	 * 用料结余
	 * @param array $post 
	 */
	public static function inventorySurplus($type,$tid)
	{
        $inventory_db = D('inventory');
    	$increaseAmount = $inventory_db->where(array('type'=>$type,'tid'=>$tid,'change'=>1))
    	             ->getField('sum(amount)');           
    	$decreaseAmount = self::inventoryDecrease($type,$tid);
        $surplus =  floatval($increaseAmount) - floatval($decreaseAmount); 
        return $surplus;
    }   

	/**
	 * 用料使用量
	 * @param array $post 
	 */
	public static function inventoryDecrease($type,$tid)
	{
        $inventory_db = D('inventory');         
    	$decreaseAmount = $inventory_db->where(array('type'=>$type,'tid'=>$tid,'change'=>0))
    	             ->getField('sum(amount)');
        return floatval($decreaseAmount);
    }

	/**
	 * 面料上下架时间
	 * @param array $pid 
	 */
	public static function suppliersUpDown($pid, $display=0)
	{
    	$suppliers_id = D('product')->where(array('id'=>$pid))->getField('suppliers_id');
    	if($suppliers_id > 0){
    		//查询是否有上架状态的使用相同面料id的产品
    		$count = D('product')->where(array('suppliers_id'=>$suppliers_id,'id'=>array('neq',$pid),'display'=>1))->count();
            if($count == 0){
	            if($display==1){ //上架
	                $editData['up_time'] = time();
	            }else{ //下架
                    $editData['down_time'] = time();
	            }  
	            D('suppliers')->where(array('suppliers_id'=>$suppliers_id))->save($editData);
	            return true;         	
            }

    	}
    	return false;
    }
	
	public static function getSuppliersByName($supplier_name){
		$Suppliers = D('suppliers')->where(array("suppliers_name"=>$supplier_name))->find();
		return $Suppliers;
	}


}